//Iditarod Trail
//Amelia Bauer, Elise Eiden, Christine Gerardi, Taryn Green
//CSE 20212 2012
/*Store window for buying supplies/dogs*/
#include "store.h"
#include "AreYouSureWindow.h"
#include "NameWindow.h"
#include "Player.h"
#include <iostream>
#include <stdio.h>
#include <QtGui>
#include <QLineEdit> 
#include <QPushButton>
#include <QApplication>
#include <QGridLayout>
#include <QString>
#include <QLabel>
#include <QImage>
#include <QPixmap>
#include <QMainWindow>
#include <QPalette>
#include <QHBoxLayout>
#include <QFont>
#include <string>
using namespace std;


Store:: Store(Player *ply, string storeName, string filename, QTimer* timer, QWidget *parent):QWidget(parent)
{
	clock = timer;
	//General variables to be used throughout
	int xSize = 700;
	int ySize = 512;
	money = ply->getMoney();
	dogC = 120;
	medC = 50;
	foodC = 2;
	ddC = 200;

	us = ply;

	//Establish which store based on input
	QString nameStart(storeName.c_str());
	QString displayStart("Welcome to ");
	QString displayEnd(" Store!");
	QString fullDisplay = displayStart.append(nameStart).append(displayEnd);
	name = new QLabel(fullDisplay);

	//Initialize window
 	setWindowTitle(tr(storeName.c_str()));
	resize(xSize,ySize);
	
	//Set the background 
	QPalette p = palette();
	QPixmap pixmap1(filename.c_str());
	QSize size(xSize,ySize);
	QPixmap pixmap(pixmap1.scaled(size));
	p.setBrush(QPalette::Background,  pixmap);
	setPalette(p);


	//Set up buying options & text
	QPushButton *pB = new QPushButton(QString("Buy!"),this);
	QPushButton *exitPB = new QPushButton(QString("Exit"),this);
	edit1 = new QLineEdit(QString("0"), this);
	edit2 = new QLineEdit(QString("0"), this);
	edit3 = new QLineEdit(QString("0"), this);
	edit4 = new QLineEdit(QString("0"), this);
	moneyTxt = new QLineEdit(QString::number(us->getMoney()));
	costTotalTxt = new QLineEdit(QString("0"));
	itemLabel = new QLabel(QString("Items"));
	costLabel = new QLabel(QString("Cost"));
	buyLabel = new QLabel(QString("Buy"));
	moneyLabel = new QLabel(QString("Your Money"));
	costTotalLabel = new QLabel(QString("Total Cost"));
	txt1 = new QLabel(QString("Dog"));
	txt2 = new QLabel(QString("Medicine (Serving Size: 2)"));
	txt3 = new QLabel(QString("Food (pounds)"));
	txt4 = new QLabel(QString("Deluxe Dog"));
	validTxt = new QLabel(QString("        \n                  "));
	dogCost = new QLabel(QString("120"));
	betterDogCost = new QLabel(QString("200"));
	medCost = new QLabel(QString("50"));
	foodCost = new QLabel(QString("2"));
	QLabel *emptyLabel = new QLabel(QString("\n\n\n\n\n\n\n\n\n\n\n    "));
	QLabel *emptyLabel2 = new QLabel(QString("                                         "));
	
	//Set up format of window
	lay = new QGridLayout(this);

	//Add spacing
	lay->addWidget(emptyLabel,0,0);
	lay->addWidget(emptyLabel2,10,0);

	//Add all previously made stuff.
	lay->addWidget(name,7,2);
	lay->addWidget(itemLabel,8,2);
	lay->addWidget(costLabel,8,3);
	lay->addWidget(buyLabel,8,4);
	lay->addWidget(moneyLabel,15,3);
	lay->addWidget(costTotalLabel, 14,3);
	lay->addWidget(txt1, 9,2);
	lay->addWidget(txt4, 10, 2);
	lay->addWidget(txt2,11,2);
	lay->addWidget(txt3,12,2);
	lay->addWidget(dogCost, 9, 3);
	lay->addWidget(betterDogCost, 10, 3);
	lay->addWidget(medCost, 11, 3);
	lay->addWidget(foodCost, 12, 3);
	lay->addWidget(moneyTxt,15,4);
	lay->addWidget(costTotalTxt,14,4);
	lay->addWidget(edit1, 9,4);
	lay->addWidget(edit4, 10,4);
	lay->addWidget(edit2, 11,4);
	lay->addWidget(edit3, 12,4);
	lay->addWidget(pB,10,1);
	lay->addWidget(validTxt,11,1);
	lay->addWidget(exitPB, 12,1);

	//Set main layout for window
	setLayout(lay);

	//Add color
	name->setStyleSheet("QLabel { background-color: darksalmon}");	
	txt1->setStyleSheet("QLabel { background-color: cornsilk}");	
	txt2->setStyleSheet("QLabel { background-color: cornsilk}");
	txt3->setStyleSheet("QLabel { background-color: cornsilk}");	
	txt4->setStyleSheet("QLabel { background-color: cornsilk}");	
	dogCost->setStyleSheet("QLabel { background-color: cornsilk}");	
	betterDogCost->setStyleSheet("QLabel { background-color: cornsilk}");	
	medCost->setStyleSheet("QLabel { background-color: cornsilk}");	
	foodCost->setStyleSheet("QLabel { background-color: cornsilk}");	
	moneyTxt->setStyleSheet("QLineEdit { background-color: cornsilk}");	
	costTotalTxt->setStyleSheet("QLineEdit { background-color: cornsilk}");	
	edit1->setStyleSheet("QLineEdit { background-color: cornsilk}");	
	edit2->setStyleSheet("QLineEdit { background-color: cornsilk}");	
	edit3->setStyleSheet("QLineEdit { background-color: cornsilk}");	
	edit4->setStyleSheet("QLineEdit { background-color: cornsilk}");	
	pB->setStyleSheet("QPushButton { background-color: cornsilk}");	
	exitPB->setStyleSheet("QPushButton { background-color: darksalmon}");
	itemLabel->setStyleSheet("QLabel { background-color: cornsilk}");	
	costLabel->setStyleSheet("QLabel { background-color: cornsilk}");	
	buyLabel->setStyleSheet("QLabel { background-color: cornsilk}");	
	moneyLabel->setStyleSheet("QLabel { background-color: cornsilk}");	
	costTotalLabel->setStyleSheet("QLabel { background-color: cornsilk}");	
	
	// Set Fonts
	pB->setFont(QFont("Arial",20,QFont::Bold));
	name->setFont(QFont("Arial",20,QFont::Bold));
	txt1->setFont(QFont("Arial",14));
	txt2->setFont(QFont("Arial",14));
	txt3->setFont(QFont("Arial",14));
	txt4->setFont(QFont("Arial",14));
	edit1->setFont(QFont("Arial",10));
	edit2->setFont(QFont("Arial",10));
	edit3->setFont(QFont("Arial",10));
	edit4->setFont(QFont("Arial",10));
	dogCost->setFont(QFont("Arial",10));
	betterDogCost->setFont(QFont("Arial",10));
	medCost->setFont(QFont("Arial",10));
	foodCost->setFont(QFont("Arial",10));
	moneyTxt->setFont(QFont("Arial",10));
	costTotalTxt->setFont(QFont("Arial",10));
	validTxt->setFont(QFont("Arial",10));
	exitPB->setFont(QFont("Arial",10));
	itemLabel->setFont(QFont("Arial",14, QFont:: Bold));
	costLabel->setFont(QFont("Arial",14, QFont:: Bold));
	buyLabel->setFont(QFont("Arial",14, QFont:: Bold));
	moneyLabel->setFont(QFont("Arial",14, QFont:: Bold));
	costTotalLabel->setFont(QFont("Arial",14, QFont:: Bold));

	//If change, call update
	connect(edit1, SIGNAL(textChanged(QString)), this, SLOT(update()));
	connect(edit2, SIGNAL(textChanged(QString)), this, SLOT(update()));
	connect(edit3, SIGNAL(textChanged(QString)), this, SLOT(update()));
	connect(edit4, SIGNAL(textChanged(QString)), this, SLOT(update()));
	connect(moneyTxt, SIGNAL(textChanged(QString)), this, SLOT(update()));
	connect(pB, SIGNAL(clicked()), this, SLOT(validate()));
	connect(exitPB, SIGNAL(clicked()), this, SLOT(openNewWindow()));

}
//Open another window for exiting decision
void Store:: openNewWindow()
{
	clock->start(3000);
	this->hide();
}
//Opens windows once bought a dog 
void Store:: boughtDogs(int numDogsNorm, int numDogsDeluxe)
{
	NameWindow *dogNameWindow = new NameWindow(NULL, numDogsNorm, numDogsDeluxe, us);
	dogNameWindow->show();
}

//Takes your money if you buy, or display error message
void Store:: validate()
{
	update();
	int numDogsNorm = edit1->text().toInt();
	int numDogsDeluxe = edit4->text().toInt();

	//You don't have that much
	if(moneyLeft<0)
	{
		validTxt->setStyleSheet("QLabel { background-color: cornsilk}");	
		validTxt->setText(QString("\t\tInvalid!\nNot enough funds."));
	}
	else if ((us->getNumDog()+numDogsNorm+numDogsDeluxe) > 8)	//8 denotes maximum number of dogs allowed
	{
		validTxt->setStyleSheet("QLabel { background-color: cornsilk}");	
		validTxt->setText(QString("\t\tInvalid!\nCannot have more than maximum dogs."));
	}
	//Buys the items 
	else
	{
		us->buyNewDogs(numDogsNorm, numDogsDeluxe);
		us->addMedicine(edit2->text().toInt());
		us->addFood(edit3->text().toInt());
		if(numDogsNorm > 0 || numDogsDeluxe>0)
			boughtDogs(numDogsNorm, numDogsDeluxe);
		money = moneyLeft;							//Changes your balance
		us->setMoney(money);
		validTxt->setStyleSheet("QLabel { background-color: transparent}");	
		validTxt->setText(QString("        \n                  "));		//Space keeper
		moneyTxt->setText(QString(QString::number(money)));
		edit1->setText(QString("0"));						//Sets items back to 0 as if shopping again
		edit2->setText(QString("0"));
		edit3->setText(QString("0"));
		edit4->setText(QString("0"));
	}
}

//Update cost with each edit 
void Store:: update()
{
	int numD, numM, numF, numDD;
	numD = edit1->text().toInt();
	numM = edit2->text().toInt();
	numF = edit3->text().toInt();
	numDD = edit4->text().toInt();

	int costTotal = numD * dogC + numF * foodC + numM * medC + numDD*ddC;
	moneyLeft = money - costTotal;
	costTotalTxt->setText(QString::number(costTotal));			
}
